package com.example.mpandroidchart1107;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class CombinedActivity extends AppCompatActivity {
    private CombinedChart combined;
    private final int count =10; //定义一个生成的数量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_combined);
        setTitle("复合图表CombinedChartActivity");
        combined = findViewById(R.id.combined);

        CombinedData data = new CombinedData();

        /*所有生成图表的方法*/
        data.setData(generateLineData());//生成折线图
       data.setData(generateBarData());//柱形图
        data.setData(generateScatterData());//散点图
        data.setData(generateBubbleData());//泡泡图
        data.setData(generateCandleData());//K线图


        Description description = new Description();//更改右下角Description 显示内容
        description.setText("复合图表");
        description.setTextSize(12f);//字体大小
        description.setTextColor(Color.parseColor("#FF6A6A"));//字体颜色
        combined.setDescription(description);

        combined.setData(data);
        combined.invalidate();

    }

    private LineData generateLineData() { //1.折线图
        LineData l = new LineData();

        ArrayList<Entry> entries = new ArrayList<>();
        for (int index = 0; index < count; index++)
            entries.add(new Entry(index + 0.5f, getRandom(15, 5)));

        LineDataSet set = new LineDataSet(entries, "折线图");
        set.setColor(Color.parseColor("#8B658B"));
        l.addDataSet(set);
        return l;
    }

    private BarData generateBarData() {//柱形图
        ArrayList<BarEntry> entries1 = new ArrayList<>();
        ArrayList<BarEntry> entries2 = new ArrayList<>();
        for (int index = 0; index < count; index++) {
            entries1.add(new BarEntry(0, getRandom(25, 25)));

            // stacked 堆栈
            entries2.add(new BarEntry(0, new float[]{getRandom(13, 12), getRandom(13, 12)}));

        }
        BarDataSet set1 = new BarDataSet(entries1, "柱1");
        set1.setColor(Color.parseColor("#FFDAB9"));
        set1.setValueTextColor(Color.parseColor("#008B8B"));
        set1.setValueTextSize(10f);
        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        BarDataSet set2 = new BarDataSet(entries2, "柱2" );
        set2.setStackLabels(new String[]{"Stack 1", "Stack 2"});
        set2.setColor(Color.parseColor("#FF6A6A"));
        set2.setValueTextColor(Color.parseColor("#00BFFF"));//数值字体颜色
        set2.setValueTextSize(10f);
        set2.setAxisDependency(YAxis.AxisDependency.LEFT);/*依赖LEFT改了会导致柱2很长*/

        float groupSpace = 0.06f;   //空间
        float barSpace = 0.04f; // x2 dataset 高
        float barWidth = 0.44f; // x2 dataset  柱形图的整个宽
        BarData d = new BarData(set1, set2);
        d.setBarWidth(barWidth);

        // make this BarData object grouped  将这个BarData对象分组
        d.groupBars(0, groupSpace, barSpace); // start at x = 0

        return d;
    }
    private ScatterData generateScatterData() {//散点图

        ScatterData d = new ScatterData();

        ArrayList<Entry> entries = new ArrayList<>();

        for (float index = 0; index < count; index += 0.5f)
            entries.add(new Entry(index + 0.25f, getRandom(10, 55)));

        ScatterDataSet set = new ScatterDataSet(entries, "散点图");
        set.setColors(ColorTemplate.MATERIAL_COLORS);
        //颜色模板 ColorTemplate   加入后会出现各种颜色，不会出现单一颜色
        set.setScatterShapeSize(7.5f); /*散射的形状大小*/
        set.setDrawValues(false);//是否显示数值
        set.setValueTextSize(10f);//数值大小
        d.addDataSet(set);

        return d;
    }

    private CandleData generateCandleData() {//k线图

        CandleData d = new CandleData();

        ArrayList<CandleEntry> entries = new ArrayList<>();

        for (int index = 0; index < count; index += 2)
            entries.add(new CandleEntry(index + 1f, 90, 70, 85, 75f));

        CandleDataSet set = new CandleDataSet(entries, "k图");
        set.setDecreasingColor(Color.rgb(142, 150, 175));
        set.setShadowColor(Color.parseColor("#6CA6CD"));//阴影颜色
        set.setColors(ColorTemplate.MATERIAL_COLORS);//颜色模板

        set.setBarSpace(0.2f);//K线图的大小数值越小，宽度越大
        set.setValueTextSize(10f);
        set.setDrawValues(true); /*数值是否显示*/
        d.addDataSet(set);

        return d;
    }
    private BubbleData generateBubbleData() {//泡泡图

        BubbleData bd = new BubbleData();

        ArrayList<BubbleEntry> entries = new ArrayList<>();

        for (int index = 0; index < count; index++) {
            float y = getRandom(10, 105);
            //改变这个数值，可以改变泡泡在图上的位置
            float size = getRandom(100, 105);
            entries.add(new BubbleEntry(index + 0.5f, y, size));
        }
        BubbleDataSet set = new BubbleDataSet(entries, "泡泡");
        set.setColors(ColorTemplate.VORDIPLOM_COLORS);
        set.setValueTextSize(10f);
        set.setValueTextColor(Color.parseColor("#8B8386"));
        set.setHighlightCircleWidth(1.5f);//高亮圆宽
        set.setDrawValues(true); //画 值
        bd.addDataSet(set);

        return bd;

    }

    protected float getRandom ( float range, float start){//获取随机数字
        return (float) (Math.random() * range) + start;

    }
}